<template>
  <div class="form-card">
    <form @submit.prevent="onSubmit">
      <div class="form-row">
        <label>{{ $t('login.idNumber') }}</label>
        <input v-model="form.idNumber" required />
      </div>
      <div class="form-row">
        <label>{{ $t('login.password') }}</label>
        <input type="password" v-model="form.password" required />
      </div>
      <div class="actions-spaced">
        <a class="link" href="#" @click.prevent="$emit('to-register')">{{ $t('login.noAccount') }}</a>
        <a class="link" href="#" @click.prevent="$emit('to-reset')">{{ $t('login.forgot') }}</a>
      </div>
      <div class="actions-center">
        <button class="btn-primary btn-lg" type="submit">{{ $t('login.submit') }}</button>
      </div>
    </form>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'LoginInline',
  data() {
    return { form: { idNumber: '', password: '' } }
  },
  methods: {
    async onSubmit() {
      try {
        const { data } = await axios.post('/api/auth/login', this.form)
        localStorage.setItem('token', data.token)
        this.$emit('success')
      } catch (e) {
        alert('Login failed')
      }
    }
  }
}
</script>

<style scoped>
</style>
